package com.epam.qanda.web.servlet.logged;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;

import com.epam.qanda.db.model.Message;
import com.epam.qanda.db.model.User;
import com.epam.qanda.db.service.MessageService;

public class ContactUsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.sendRedirect("pmain");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String message = request.getParameter("msg");
		message = StringEscapeUtils.escapeHtml4(message);
		User user = (User) request.getSession().getAttribute("user");
		Message msg = new Message();
		msg.setText(message);
		msg.setUserId(user.getId());
		msg.setUserLogin(user.getLogin());
		new MessageService().create(msg);
		response.sendRedirect(request.getHeader("Referer"));
	}
}
